<?php

include_once(drupal_get_path('theme', 'minduke_studio') . '/common.inc');

function minduke_studio_theme() {
  $items = array();
  $items['render_panel'] = array(
    "variables" => array(
      'page' => array(),
      'panels_list' => array(),
      'panel_regions_width' => array(),
    ),
    'preprocess functions' => array(
      'minduke_studio_preprocess_render_panel'
    ),
    'template' => 'panel',
    'path' => drupal_get_path('theme', 'minduke_studio') . '/templates',
  );
  return $items;
}


function minduke_studio_preprocess_page(&$vars) {
  global $theme_key;

  $vars['regions_width'] = minduke_studio_regions_width($vars['page']);
  $panel_regions = minduke_studio_panel_regions();
  if (count($panel_regions)) {
    foreach ($panel_regions as $panel_name => $panels_list) {
      $panel_markup = theme("render_panel", array(
        'page' => $vars['page'],
        'panels_list' => $panels_list,
        'regions_width' => $vars['regions_width'],
      ));
      $panel_markup = trim($panel_markup);
      $vars['page'][$panel_name] = empty($panel_markup) ? FALSE : array('content' => array('#markup' => $panel_markup));
    }
  }
}

function minduke_studio_preprocess_render_panel(&$variables) {
  $page = $variables['page'];
  $panels_list = $variables['panels_list'];
  $regions_width = $variables['regions_width'];
  $variables = array();
  $variables['page'] = array();
  $variables['panel_width'] = $regions_width;
  $variables['panel_classes'] = array();
  $variables['panels_list'] = $panels_list;
  $is_empty = TRUE;
  $panel_keys = array_keys($panels_list);

  foreach ($panels_list as $panel) {
    $variables['page'][$panel] = $page[$panel];
    $panel_width = $regions_width[$panel];
    if (render($page[$panel])) {
      $is_empty = FALSE;
    }
    $classes = array("panel-column");
    $classes[] = "col-md-$panel_width";
    $classes[] = str_replace("_", "-", $panel);
    $variables['panel_classes'][$panel] = implode(" ", $classes);
  }
  $variables['empty_panel'] = $is_empty;
}

function minduke_studio_form_alter(&$form, &$form_state, $form_id){
  if ($form_id == 'webform_client_form_10') {
    $form['actions']['submit']['#attributes']['value'][] = 'Send message';
  }
}

function minduke_studio_preprocess_field(&$variables) {
  if ($variables['element']['#field_name'] == 'field_level') {
    $progress_vl = $variables['element']['#items'][0]['value'];
    $data['progress_vl'] = $progress_vl;
    $theme_path = drupal_get_path('theme', 'minduke_studio');
    $variables['items'][0]['#markup'] = theme_render_template($theme_path . '/templates/progress.tpl.php', $data);
  }
}